﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLHXNK.BusinessLogic;

namespace QLHXNK
{
    public partial class FrmBaoCaoNhapHang : Form
    {
        private int ItemID = -1;

        public FrmBaoCaoNhapHang()
        {
            InitializeComponent();
            this.CenterToScreen();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void FrmBaoCaoNhapHang_Load(object sender, EventArgs e)
        {
            //Load DS ma hang:
            getItemList();

        }

        private void getItemList()
        {
            DataTable table = Item.ListItem();

            if (table == null)
            {
                table = new DataTable();
                table.Columns.Add("item_code");
                table.Columns.Add("item_name");
                table.Columns.Add("ID");
            }

            DataRow row = table.NewRow();
            row["ID"] = -1;
            row["item_name"] = "Chọn hàng hóa";
            row["item_code"] = "";
            table.Rows.InsertAt(row, 0);

            cmbMaHang.ValueMember = "ID";
            cmbMaHang.DisplayMember = "item_name";
            cmbMaHang.DataSource = table;
        }

        private void cmbMaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ItemID = int.Parse(cmbMaHang.SelectedValue.ToString());
            }
            catch
            {
                ItemID = -1;
            }
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            if (ValidateData() == false)
                return;
            doPrintReport();
        }
        private void doPrintReport()
        {
            DateTime fromDate = dtpickerTuNgay.Value;
            DateTime toDate = dtpickerDenNgay.Value;

            FrmInBaoCao frm = new FrmInBaoCao(fromDate,toDate,ItemID);
            frm.ShowDialog();
        }

        private bool ValidateData()
        {
            bool isValidate = true;
            DateTime startDate = dtpickerTuNgay.Value.Date;
            DateTime endDate = dtpickerDenNgay.Value.Date;
            if (startDate > endDate)
            {
                errorProvider1.SetError(dtpickerDenNgay, "Ngày kết thúc không thể trước ngày bắt đầu");
                isValidate = false;
            }
            return isValidate;
        }

        private void dtpickerTuNgay_ValueChanged(object sender, EventArgs e)
        {
            DateTime startDate = dtpickerTuNgay.Value.Date;
            DateTime endDate = dtpickerDenNgay.Value.Date;
            if (startDate <= endDate)
                errorProvider1.SetError(dtpickerDenNgay, "");
        }

        private void dtpickerDenNgay_ValueChanged(object sender, EventArgs e)
        {
            DateTime startDate = dtpickerTuNgay.Value.Date;
            DateTime endDate = dtpickerDenNgay.Value.Date;
            if (startDate <= endDate)
                errorProvider1.SetError(dtpickerDenNgay, "");
        }

        private void dtpickerTuNgay_KeyDown(object sender, KeyEventArgs e)
        {
            FrmBaoCaoNhapHang_KeyDown(this, e);
        }

        private void dtpickerDenNgay_KeyDown(object sender, KeyEventArgs e)
        {
            FrmBaoCaoNhapHang_KeyDown(this, e);
        }

        private void cmbMaHang_KeyDown(object sender, KeyEventArgs e)
        {
            FrmBaoCaoNhapHang_KeyDown(this, e);
        }

        private void btnBaoCao_KeyDown(object sender, KeyEventArgs e)
        {
            FrmBaoCaoNhapHang_KeyDown(this, e);
        }

        private void FrmBaoCaoNhapHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
                this.Close();
        }
    }
}